package com.gy.algorithm.carl.hashtableexa;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: javabase
 * @BelongsPackage: com.gy.algorithm.carl
 * @Author: gy
 * @Date: 2024/10/22 11:30
 * @Description:
 */
public class FourSumCountwoSum454 {

    public static void main(String[] args) {
        System.out.println(fourSumCount(new int[]{1,2},new int[]{-2,-1},new int[]{-1,2},new int[]{0,2}));
    }

    // end1
    public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer,Integer> count = new HashMap<>();
        for (int i : nums1) {
            for (int j : nums2) {
                count.put(i + j,count.getOrDefault(i+j,0) + 1);
            }
        }

        int res = 0;
        for (int i : nums3) {
            for (int j : nums4) {
                res += count.getOrDefault(0-i-j,0);
            }
        }

        return res;
    }

    // write
    public static int fourSumCount1(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int res = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                for (int k = 0; k < nums3.length; k++) {
                    for (int l = 0; l < nums4.length; l++) {
                        if(nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0){
                            res++;
                        }
                    }
                }
            }
        }
        return res;
    }
}
